import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

public class WordCount {
    public static void main(String[] args) throws Exception {
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        String inputPath = "E:\\codes\\idealJava\\flinke\\src\\main\\resources\\hello.txt";
        DataSet<String> inputDataSet = env.readTextFile(inputPath);

        DataSet<Tuple2<String,Integer>> wordCountDataSet =  inputDataSet.flatMap(new FlatMapMapper()).groupBy(0).sum(1);
        wordCountDataSet.print();
    }

    public static class FlatMapMapper implements FlatMapFunction<String,Tuple2<String,Integer>>{

        @Override
        public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
            String[] split = value.split(" ");
            for (String s : split) {
                out.collect(new Tuple2<>(s,1));
            }
        }
    }


}
